#ifdef DHT11
//*********************************************************************************************/
// Defines for variables used by this userplugin
//*********************************************************************************************/

#define DHT11_SEND_DELAY_MS        500  // Delay in msec before humidity data is send through RF (after temperature data has been send)

//===================================================================================================================================
// End of user configurable settings, do not change anything below this section!
//===================================================================================================================================

void UserPlugin_DHT_Command(int Par1, int Par2)
{
  if (Par1 == 145) dhtread(Par2);
}

#if NODO_MEGA
/*********************************************************************************************\
 * dht 11 functies voor Mega
 \*********************************************************************************************/

#define DHT11_PIN 7     // connect data lead to pin 30 on Mega Board (PC7)

void dhtread(byte Par2)
{
  byte dht11_dat[5];
  byte dht11_in;
  byte i;
  unsigned long customEvent=0;

  DDRC |= _BV(DHT11_PIN);
  PORTC |= _BV(DHT11_PIN);

  // start condition
  // 1. pull-down i/o pin from 18ms
  PORTC &= ~_BV(DHT11_PIN);
  delay(18);
  PORTC |= _BV(DHT11_PIN);
  delayMicroseconds(40);
  DDRC &= ~_BV(DHT11_PIN);
  delayMicroseconds(40);

  dht11_in = PINC & _BV(DHT11_PIN);
  if(dht11_in)
  {
    sprintf(TempString,"%s","dht11 E1");
    userPluginPrintTerminal(TempString);
    return;
  }
  delayMicroseconds(80);
  dht11_in = PINC & _BV(DHT11_PIN);
  if(!dht11_in)
  {
    sprintf(TempString,"%s","dht11 E2");
    userPluginPrintTerminal(TempString);
    return;
  }

  delayMicroseconds(80);// now ready for data reception
  for (i=0; i<5; i++)
    dht11_dat[i] = read_dht11_dat();
  DDRC |= _BV(DHT11_PIN);
  PORTC |= _BV(DHT11_PIN);
  byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];// check check_sum
  if(dht11_dat[4]!= dht11_check_sum)
  {
    sprintf(TempString,"%s","dht11 E3");
    userPluginPrintTerminal(TempString);
  }

  sprintf(TempString,"%s","Temperature:");
  strcat(TempString,int2str(dht11_dat[2]));
  userPluginPrintTerminal(TempString);
  customEvent=float2event((float)dht11_dat[2], Par2 -1, CMD_VARIABLE_EVENT);
  SendHTTPEvent(customEvent);
  PrintEvent(customEvent,VALUE_SOURCE_HTTP,VALUE_DIRECTION_OUTPUT);

  sprintf(TempString,"%s","Humidity:");
  strcat(TempString,int2str(dht11_dat[0]));
  userPluginPrintTerminal(TempString);
  customEvent=float2event((float)dht11_dat[0], Par2, CMD_VARIABLE_EVENT);
  SendHTTPEvent(customEvent);
  PrintEvent(customEvent,VALUE_SOURCE_HTTP,VALUE_DIRECTION_OUTPUT);

}

byte read_dht11_dat()
{
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++)
  {
    while(!(PINC & _BV(DHT11_PIN)));  // wait for 50us
    delayMicroseconds(30);
    if(PINC & _BV(DHT11_PIN)) 
      result |=(1<<(7-i));
    while((PINC & _BV(DHT11_PIN)));  // wait '1' finish
  }
  return result;
}

#else

/*********************************************************************************************\
 * dht 11 functies voor de Due/Uno
 \*********************************************************************************************/

#define DHT11_PIN 7     // connect data lead to pin 7 on Uno Board (PD7)

void dhtread(byte Par2)
{
  byte dht11_dat[5];
  byte dht11_in;
  byte i;

  DDRD |= _BV(DHT11_PIN);
  PORTD |= _BV(DHT11_PIN);

  // start condition
  // 1. pull-down i/o pin from 18ms
  PORTD &= ~_BV(DHT11_PIN);
  delay(18);
  PORTD |= _BV(DHT11_PIN);
  delayMicroseconds(40);
  DDRD &= ~_BV(DHT11_PIN);
  delayMicroseconds(40);

  dht11_in = PIND & _BV(DHT11_PIN);
  if(dht11_in)
  {
    sprintf(TempString,"%s","dht11 E1");
    userPluginPrintTerminal(TempString);
    return;
  }
  delayMicroseconds(80);
  dht11_in = PIND & _BV(DHT11_PIN);
  if(!dht11_in)
  {
    sprintf(TempString,"%s","dht11 E2");
    userPluginPrintTerminal(TempString);
    return;
  }

  delayMicroseconds(80);// now ready for data reception
  for (i=0; i<5; i++)
    dht11_dat[i] = read_dht11_dat();
  DDRD |= _BV(DHT11_PIN);
  PORTD |= _BV(DHT11_PIN);
  byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];// check check_sum
  if(dht11_dat[4]!= dht11_check_sum)
  {
    sprintf(TempString,"%s","dht11 E3");
    userPluginPrintTerminal(TempString);
  }

  sprintf(TempString,"%s","Temperature:");
  strcat(TempString,int2str(dht11_dat[2]));
  userPluginPrintTerminal(TempString);
  Nodo_2_RawSignal(float2event((float)dht11_dat[2], Par2 -1, CMD_VARIABLE_EVENT));
  RawSendRF();
  delay(DHT11_SEND_DELAY_MS);

  sprintf(TempString,"%s","Humidity:");
  strcat(TempString,int2str(dht11_dat[0]));
  userPluginPrintTerminal(TempString);
  Nodo_2_RawSignal(float2event((float)dht11_dat[0], Par2, CMD_VARIABLE_EVENT));
  RawSendRF();

}

byte read_dht11_dat()
{
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++)
  {
    while(!(PIND & _BV(DHT11_PIN)));  // wait for 50us
    delayMicroseconds(30);
    if(PIND & _BV(DHT11_PIN)) 
      result |=(1<<(7-i));
    while((PIND & _BV(DHT11_PIN)));  // wait '1' finish
  }
  return result;
}

#endif

#endif

